﻿using OrderSystem.Common;
using OrderSystem.DataAccess;
using OrderSystem.Templet;
using System;
using System.ComponentModel;
using System.Data.SqlClient;
using System.Drawing;
using System.Windows.Forms;

namespace OrderSystem
{
    public partial class FormQueryOrders : FormTemplet
    {
        private int _nCustomerID = -1;

        private int _nProjectID = -1;

        public FormQueryOrders()
        {
            this.InitializeComponent();
        }

        private void FormQueryOrders_Load(object sender, EventArgs e)
        {
            this.dtpSpeakForDateStart.Enabled = false;
            this.dtpSpeakForDateEnd.Enabled = false;
            this.dtpDeliveryDateStart.Enabled = false;
            this.dtpDeliveryDateEnd.Enabled = false;
            this.ckLailiao.Enabled = false;
            this.ckQuliao.Enabled = false;
            this.chkLockedOrderData.Enabled = false;
            this.chkCancelledOrder.Enabled = false;
            this.dtpPayedDateStart.Enabled = false;
            this.dtpPayedDateEnd.Enabled = false;
            if (!LoginInfo.Role[PowerName.Order_Delete])
            {
                this.btnDeleteOrder.Enabled = false;
            }
        }

        private void btnSelectCustomer_Click(object sender, EventArgs e)
        {
            CustomerData customerData = FormSelectCustomer.ShowBox();
            if (customerData != null)
            {
                this._nCustomerID = customerData.CustomerID;
                this.txtCustomerName.Text = customerData.CompanyName + "(" + customerData.Linkman + ")";
                this._nProjectID = -1;
                this.txtProjectName.Text = "";
            }
        }

        private void btnSelectProject_Click(object sender, EventArgs e)
        {
            ProjectData projectData = FormSelectProject.ShowBox(this._nCustomerID);
            if (projectData != null)
            {
                this._nProjectID = projectData.ProjectID;
                this.txtProjectName.Text = projectData.ProjectName + "(" + projectData.Linkman + ")";
            }
        }

        private void ckLailiaoUsed_CheckedChanged(object sender, EventArgs e)
        {
            if (this.ckLailiaoUsed.Checked)
            {
                this.ckLailiao.Enabled = true;
            }
            else
            {
                this.ckLailiao.Enabled = false;
            }
        }

        private void ckQuliaoUsed_CheckedChanged(object sender, EventArgs e)
        {
            if (this.ckQuliaoUsed.Checked)
            {
                this.ckQuliao.Enabled = true;
            }
            else
            {
                this.ckQuliao.Enabled = false;
            }
        }

        private void chkSpeakForDateUsed_CheckedChanged(object sender, EventArgs e)
        {
            if (this.chkSpeakForDateUsed.Checked)
            {
                this.dtpSpeakForDateStart.Enabled = true;
                this.dtpSpeakForDateEnd.Enabled = true;
            }
            else
            {
                this.dtpSpeakForDateStart.Enabled = false;
                this.dtpSpeakForDateEnd.Enabled = false;
            }
        }

        private void chkDeliveryDateUsed_CheckedChanged(object sender, EventArgs e)
        {
            if (this.chkDeliveryDateUsed.Checked)
            {
                this.dtpDeliveryDateStart.Enabled = true;
                this.dtpDeliveryDateEnd.Enabled = true;
            }
            else
            {
                this.dtpDeliveryDateStart.Enabled = false;
                this.dtpDeliveryDateEnd.Enabled = false;
            }
        }

        private void ckbPayDateUsed_CheckedChanged(object sender, EventArgs e)
        {
            if (this.ckbPayedDateUsed.Checked)
            {
                this.dtpPayedDateStart.Enabled = true;
                this.dtpPayedDateEnd.Enabled = true;
            }
            else
            {
                this.dtpPayedDateStart.Enabled = false;
                this.dtpPayedDateEnd.Enabled = false;
            }
        }

        private void chkLockedOrderDataUsed_CheckedChanged(object sender, EventArgs e)
        {
            if (this.chkLockedOrderDataUsed.Checked)
            {
                this.chkLockedOrderData.Enabled = true;
            }
            else
            {
                this.chkLockedOrderData.Enabled = false;
            }
        }

        private void chkCancelledOrderUsed_CheckedChanged(object sender, EventArgs e)
        {
            if (this.chkCancelledOrderUsed.Checked)
            {
                this.chkCancelledOrder.Enabled = true;
            }
            else
            {
                this.chkCancelledOrder.Enabled = false;
            }
        }

        private void txtSalesperson_DoubleClick(object sender, EventArgs e)
        {
            string text = FormSelectSalesperson.ShowBox("请选择业务员...");
            if (text != null)
            {
                this.txtSalesperson.Text = text.Trim();
            }
        }

        private OrderCondition GetConditionForQuery()
        {
            OrderCondition orderCondition = new OrderCondition();
            OrderCondition result;
            if (this.txtCustomerName.Text.Trim() == "" && this.txtProjectName.Text.Trim() == "" && !this.chkSpeakForDateUsed.Checked && !this.chkDeliveryDateUsed.Checked && !this.ckLailiaoUsed.Checked && !this.ckQuliao.Checked && !this.chkLockedOrderDataUsed.Checked && !this.chkCancelledOrderUsed.Checked && !this.ckbPayedDateUsed.Checked && this.txtOrderCode.Text.Trim() == "" && this.txtRemark.Text.Trim() == "" && this.txtSalesperson.Text.Trim() == "")
            {
                MessageBox.Show("至少要输入一条查询信息", "提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                result = null;
            }
            else
            {
                if (this.txtCustomerName.Text.Trim() != "")
                {
                    if (this.txtCustomerName.Text.Trim() == "" || this._nCustomerID == -1)
                    {
                        MessageBox.Show("请选择客户", "提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                        this.txtCustomerName.Focus();
                        result = null;
                        return result;
                    }
                    orderCondition.CustomerID = this._nCustomerID;
                }
                if (this.txtProjectName.Text.Trim() != "")
                {
                    if (this.txtProjectName.Text.Trim() == "" || this._nProjectID == -1)
                    {
                        MessageBox.Show("请选择计划", "提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                        this.txtProjectName.Focus();
                        result = null;
                        return result;
                    }
                    orderCondition.ProjectID = this._nProjectID;
                }
                if (this.chkSpeakForDateUsed.Checked)
                {
                    int num = DateTime.Parse(this.dtpSpeakForDateStart.Value.ToString("yyyy-MM-dd") + " 00:00:00").CompareTo(DateTime.Parse(this.dtpSpeakForDateEnd.Value.ToString("yyyy-MM-dd") + " 23:59:59"));
                    if (num > 0)
                    {
                        MessageBox.Show("订购日期：后面的日期要大于或等于前面的日期", "错误", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                        this.dtpSpeakForDateEnd.Focus();
                        result = null;
                        return result;
                    }
                    orderCondition.SpeakForDateStart = this.dtpSpeakForDateStart.Value.ToString("yyyy-MM-dd") + " 00:00:00";
                    orderCondition.SpeakForDateEnd = this.dtpSpeakForDateEnd.Value.ToString("yyyy-MM-dd") + " 23:59:59";
                }
                if (this.chkDeliveryDateUsed.Checked)
                {
                    int num = DateTime.Parse(this.dtpDeliveryDateStart.Value.ToString("yyyy-MM-dd") + " 00:00:00").CompareTo(DateTime.Parse(this.dtpDeliveryDateEnd.Value.ToString("yyyy-MM-dd") + " 23:59:59"));
                    if (num > 0)
                    {
                        MessageBox.Show("交货日期：后面的日期要大于或等于前面的日期", "错误", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                        this.dtpSpeakForDateEnd.Focus();
                        result = null;
                        return result;
                    }
                    orderCondition.DeliveryDateStart = this.dtpDeliveryDateStart.Value.ToString("yyyy-MM-dd") + " 00:00:00";
                    orderCondition.DeliveryDateEnd = this.dtpDeliveryDateEnd.Value.ToString("yyyy-MM-dd") + " 23:59:59";
                }
                if (this.ckLailiaoUsed.Checked)
                {
                    if (this.ckLailiao.Checked)
                    {
                        orderCondition.IsLailiao = 1;
                    }
                    else
                    {
                        orderCondition.IsLailiao = 0;
                    }
                }
                if (this.ckQuliaoUsed.Checked)
                {
                    if (this.ckQuliao.Checked)
                    {
                        orderCondition.IsQuliao = 1;
                    }
                    else
                    {
                        orderCondition.IsQuliao = 0;
                    }
                }
                if (this.chkLockedOrderDataUsed.Checked)
                {
                    if (this.chkLockedOrderData.Checked)
                    {
                        orderCondition.Locked = 1;
                    }
                    else
                    {
                        orderCondition.Locked = 0;
                    }
                }
                if (this.chkCancelledOrderUsed.Checked)
                {
                    if (this.chkCancelledOrder.Checked)
                    {
                        orderCondition.Cancelled = 1;
                    }
                    else
                    {
                        orderCondition.Cancelled = 0;
                    }
                }
                if (this.ckbPayedDateUsed.Checked)
                {
                    int num = DateTime.Parse(this.dtpPayedDateStart.Value.ToString("yyyy-MM-dd") + " 00:00:00").CompareTo(DateTime.Parse(this.dtpPayedDateEnd.Value.ToString("yyyy-MM-dd") + " 23:59:59"));
                    if (num > 0)
                    {
                        MessageBox.Show("付款日期：后面的日期要大于或等于前面的日期", "提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                        this.dtpPayedDateEnd.Focus();
                        result = null;
                        return result;
                    }
                    orderCondition.PayDateStart = this.dtpPayedDateStart.Value.ToString("yyyy-MM-dd") + " 00:00:00";
                    orderCondition.PayDateEnd = this.dtpPayedDateEnd.Value.ToString("yyyy-MM-dd") + " 23:59:59";
                }
                if (this.txtOrderCode.Text.Trim() != "")
                {
                    orderCondition.OrderCode = this.txtOrderCode.Text.Trim();
                }
                if (this.txtRemark.Text.Trim() != "")
                {
                    if (this.txtRemark.Text.Trim().Length > 64)
                    {
                        MessageBox.Show("字符数量不能大于64", "提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                        this.txtRemark.Focus();
                        result = null;
                        return result;
                    }
                    orderCondition.Remark = this.txtRemark.Text.Trim();
                }
                if (this.txtSalesperson.Text.Trim() != "")
                {
                    if (this.txtSalesperson.Text.Trim().Length > 16)
                    {
                        MessageBox.Show("字符数量不能大于16", "提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                        this.txtSalesperson.Focus();
                        result = null;
                        return result;
                    }
                    orderCondition.Salesperson = this.txtSalesperson.Text.Trim();
                }
                result = orderCondition;
            }
            return result;
        }

        private void dtpSpeakForDateStart_ValueChanged(object sender, EventArgs e)
        {
            int num = DateTime.Parse(this.dtpSpeakForDateStart.Value.ToString("yyyy-MM-dd") + " 00:00:00").CompareTo(DateTime.Parse(this.dtpSpeakForDateEnd.Value.ToString("yyyy-MM-dd") + " 23:59:59"));
            if (num > 0)
            {
                this.dtpSpeakForDateEnd.Value = this.dtpSpeakForDateStart.Value;
            }
        }

        private void dtpSpeakForDateEnd_ValueChanged(object sender, EventArgs e)
        {
            int num = DateTime.Parse(this.dtpSpeakForDateStart.Value.ToString("yyyy-MM-dd") + " 00:00:00").CompareTo(DateTime.Parse(this.dtpSpeakForDateEnd.Value.ToString("yyyy-MM-dd") + " 23:59:59"));
            if (num > 0)
            {
                this.dtpSpeakForDateStart.Value = this.dtpSpeakForDateEnd.Value;
            }
        }

        private void dtpDeliveryDateStart_ValueChanged(object sender, EventArgs e)
        {
            int num = DateTime.Parse(this.dtpDeliveryDateStart.Value.ToString("yyyy-MM-dd") + " 00:00:00").CompareTo(DateTime.Parse(this.dtpDeliveryDateEnd.Value.ToString("yyyy-MM-dd") + " 23:59:59"));
            if (num > 0)
            {
                this.dtpDeliveryDateEnd.Value = this.dtpDeliveryDateStart.Value;
            }
        }

        private void dtpDeliveryDateEnd_ValueChanged(object sender, EventArgs e)
        {
            int num = DateTime.Parse(this.dtpDeliveryDateStart.Value.ToString("yyyy-MM-dd") + " 00:00:00").CompareTo(DateTime.Parse(this.dtpDeliveryDateEnd.Value.ToString("yyyy-MM-dd") + " 23:59:59"));
            if (num > 0)
            {
                this.dtpDeliveryDateStart.Value = this.dtpDeliveryDateEnd.Value;
            }
        }

        private void dtpPayedDateStart_ValueChanged(object sender, EventArgs e)
        {
            int num = DateTime.Parse(this.dtpPayedDateStart.Value.ToString("yyyy-MM-dd") + " 00:00:00").CompareTo(DateTime.Parse(this.dtpPayedDateEnd.Value.ToString("yyyy-MM-dd") + " 23:59:59"));
            if (num > 0)
            {
                this.dtpPayedDateEnd.Value = this.dtpPayedDateStart.Value;
            }
        }

        private void dtpPayedDateEnd_ValueChanged(object sender, EventArgs e)
        {
            int num = DateTime.Parse(this.dtpPayedDateStart.Value.ToString("yyyy-MM-dd") + " 00:00:00").CompareTo(DateTime.Parse(this.dtpPayedDateEnd.Value.ToString("yyyy-MM-dd") + " 23:59:59"));
            if (num > 0)
            {
                this.dtpPayedDateStart.Value = this.dtpPayedDateEnd.Value;
            }
        }

        private void btnDeleteOrder_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("确定删除数据？", "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Question) != DialogResult.Cancel)
            {
                OrderCondition conditionForQuery = this.GetConditionForQuery();
                if (conditionForQuery != null)
                {
                    Orders orders = new Orders();
                    try
                    {
                        bool flag = orders.Delete(conditionForQuery);
                    }
                    catch (SqlException ex)
                    {
                        MessageBox.Show(ex.Message + "\r\n\r\n或者是连接数据库的通道出现故障，请检查！\r\n\r\n订单项信息可能没有读出来", "错误", MessageBoxButtons.OK, MessageBoxIcon.Hand);
                    }
                    catch (Exception ex2)
                    {
                        MessageBox.Show(ex2.Message + "\r\n\r\n订单信息可能没有读出来", "错误", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                    }
                    orders.Dispose();
                }
            }
        }

        private void btnOk_Click(object sender, EventArgs e)
        {
            OrderCondition conditionForQuery = this.GetConditionForQuery();
            if (conditionForQuery != null)
            {
                FormOrderList formOrderList = new FormOrderList(conditionForQuery);
                if (formOrderList != null && !formOrderList.IsDisposed)
                {
                    formOrderList.MdiParent = base.MdiParent;
                    formOrderList.Show();
                    formOrderList.Activate();
                }
            }
        }

        private void btnPayments_Click(object sender, EventArgs e)
        {
            if (!LoginInfo.Role[PowerName.Order_Payed])
            {
                MessageBox.Show("你没有操作这里的权限！", "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            }
            else
            {
                OrderCondition conditionForQuery = this.GetConditionForQuery();
                if (conditionForQuery != null)
                {
                    FormPaymentList formPaymentList = new FormPaymentList(conditionForQuery);
                    if (formPaymentList != null && !formPaymentList.IsDisposed)
                    {
                        formPaymentList.MdiParent = base.MdiParent;
                        formPaymentList.Show();
                        formPaymentList.Activate();
                    }
                }
            }
        }

        private void btnShipmentMessage_Click(object sender, EventArgs e)
        {
            OrderCondition conditionForQuery = this.GetConditionForQuery();
            if (conditionForQuery != null)
            {
                FormOrderShipmentMessage formOrderShipmentMessage = new FormOrderShipmentMessage(conditionForQuery);
                if (formOrderShipmentMessage != null && !formOrderShipmentMessage.IsDisposed)
                {
                    formOrderShipmentMessage.MdiParent = base.MdiParent;
                    formOrderShipmentMessage.Show();
                    formOrderShipmentMessage.Activate();
                }
            }
        }

        private void btnOrderDetailForTempered_Click(object sender, EventArgs e)
        {
            OrderCondition conditionForQuery = this.GetConditionForQuery();
            if (conditionForQuery != null)
            {
                FormOrderDetailForTempered formOrderDetailForTempered = new FormOrderDetailForTempered(conditionForQuery);
                if (formOrderDetailForTempered != null && !formOrderDetailForTempered.IsDisposed)
                {
                    formOrderDetailForTempered.MdiParent = base.MdiParent;
                    formOrderDetailForTempered.Show();
                    formOrderDetailForTempered.Activate();
                }
            }
        }

        private void btnInvoiceDetailOfOrder_Click(object sender, EventArgs e)
        {
            OrderCondition conditionForQuery = this.GetConditionForQuery();
            if (conditionForQuery != null)
            {
                FormInvoiceDetailOfOrder formInvoiceDetailOfOrder = new FormInvoiceDetailOfOrder(conditionForQuery);
                if (formInvoiceDetailOfOrder != null && !formInvoiceDetailOfOrder.IsDisposed)
                {
                    formInvoiceDetailOfOrder.MdiParent = base.MdiParent;
                    formInvoiceDetailOfOrder.Show();
                    formInvoiceDetailOfOrder.Activate();
                }
            }
        }

        private void btnShowSumUp_Click(object sender, EventArgs e)
        {
            OrderCondition conditionForQuery = this.GetConditionForQuery();
            if (conditionForQuery != null)
            {
                FormOrderSumUp formOrderSumUp = new FormOrderSumUp(conditionForQuery);
                if (formOrderSumUp != null && !formOrderSumUp.IsDisposed)
                {
                    formOrderSumUp.MdiParent = base.MdiParent;
                    formOrderSumUp.Show();
                    formOrderSumUp.Activate();
                }
            }
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            base.Close();
        }
    }
}
